<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/' + goods.categories[0].id">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/' + goods.categories[1].id">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
          <!-- 名字区域 -->
          <GoodName :goods="goods"></GoodName>
          <!-- 规格组件 -->
          <GoodsSku :goods="goods" @change="changeSku"></GoodsSku>
          <!-- 数量选择组件 -->
          <XtxNumbox
            v-model="number"
            :max="goods.inventory"
            label="数量"
          ></XtxNumbox>
          <!-- 按钮 -->
          <XtxButton size="large" type="primary" @click="insertCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant></GoodsRelevant>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs"></div>
          <!-- 注意事项 -->
          <div class="goods-warn"></div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsSku from './components/goods-sku.vue'
import GoodsRelevant from './components/goods-relevant.vue'
import GoodsImage from './components/Goods-Imagea.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import { findGoods } from '@/api/product'
import { useRoute } from 'vue-router'
import { ref, watch, nextTick } from 'vue'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'

export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodName, GoodsSku },
  setup() {
    // 获取选择规格的信息
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      // 可能有有数据，可能没数据
      currSku.value = sku
    }
    // 1.获取商品详情，进行商品渲染
    const goods = useGoods()
    // 选择的数量
    const number = ref(1)
    const currSku = ref(null)
    const store = useStore()

    // 加入购物车
    const insertCart = () => {
      // id skuID name attrsText picture price nowPrice selected stock count isEffective
      // 后台需要的数据，购物车
      const {
        skuId,
        specsText: attrsText,
        inventory: stock
      } = currSku.value !== null && currSku.value
      const { name, price, mainPictures, id } =
        currSku.value !== null && goods.value
      if (currSku.value && currSku.value.skuId) {
        store
          .dispatch('cart/insertCart', {
            id,
            skuId,
            attrsText,
            stock,
            price,
            nowPrice: price,
            name,
            // 图片
            picture: mainPictures[0],
            selected: true,
            isEffective: true,
            count: number.value
          })
          .then(() => {
            Message('success', '加入购物车成功')
          })
      } else {
        Message('warn', '请选择完整的规格')
      }
    }
    return {
      goods,
      changeSku,
      number,
      insertCart
    }
  }
}
// 获取商品详情
const useGoods = () => {
  // 出现路由地址商品ID发生变化，但是不会重新初始化组件
  const goods = ref(null)
  const route = useRoute()
  watch(
    () => route.params.id,
    (newVal) => {
      if (newVal && `/product/${newVal}` === route.path) {
        findGoods(route.params.id).then((data) => {
          // 让商品数据为null让后使用v-if的组件可以重新销毁和创建
          goods.value = null
          nextTick(() => {
            goods.value = data.result
          })
        })
      }
    },
    { immediate: true }
  )
  return goods
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.xtx-button {
  margin-top: 20px;
}
</style>
